package bev.frames.client;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
import bev.frames.login.LoginFrame;

public class MenuBar {
    ClientFrame parentFrame;
    JMenuBar menuBar;

    // Returns the JMenuBar instance itself so it can
    // be attached to a frame (e.g. ClientFrame).
    public JMenuBar getMenuBar() {
        return menuBar;
    }

    public MenuBar( ClientFrame parentFrame ) {
        this.parentFrame = parentFrame;

        menuBar = new JMenuBar();
        menuBar.add( buildFileMenu( parentFrame ) );
        //Not implemented menuBar.add( buildEditMenu() ); 
        //Not implemented menuBar.add( buildToolsMenu() );
        //Not implemented menuBar.add( buildHelpMenu() );
    }

    // File menu.
    private JMenu buildFileMenu( ClientFrame parentFrame ) {
        JMenu fileMenu = new JMenu( "File" );

        // Connect (Sub Menu).
        fileMenu.add( buildConnectSubMenu() );

        fileMenu.addSeparator();

        // Exit.
        JMenuItem fileExit = new JMenuItem( "Exit" );
        fileExit.addActionListener( new ExitClient() );
        fileMenu.add( fileExit );

        return fileMenu;
    }

    // File->Connect sub menu.
    private JMenu buildConnectSubMenu() {
        JMenu connectSubMenu = new JMenu( "Connect" );

        // New Project.
        JMenuItem connectNew = new JMenuItem( "New Project" );
        connectNew.setAccelerator( KeyStroke.getKeyStroke(
                java.awt.event.KeyEvent.VK_N, java.awt.Event.CTRL_MASK ) );
        connectNew.addActionListener( new ConnectToNewProject() );
        connectSubMenu.add( connectNew );

        // Existing Project.
        JMenuItem connectExisting = new JMenuItem( "Existing Project" );
        connectExisting.setAccelerator( KeyStroke.getKeyStroke(
                java.awt.event.KeyEvent.VK_O, java.awt.Event.CTRL_MASK ) );
        connectExisting.addActionListener( new OpenConnectionFile() );
        connectSubMenu.add( connectExisting );

        return connectSubMenu;
    }
    /* Not implemented
    // Edit menu.
    private JMenu buildEditMenu() {
        JMenu editMenu = new JMenu( "Edit" );

        return editMenu;
    }

    // Tools menu.
    private JMenu buildToolsMenu() {
        JMenu toolsMenu = new JMenu( "Tools" );

        return toolsMenu;
    }

    // Help menu.
    private JMenu buildHelpMenu() {
        JMenu helpMenu = new JMenu( "Help" );

        // About.
        JMenuItem helpAbout = new JMenuItem( "About" );
        helpAbout.addActionListener( new AboutDialog() );
        helpMenu.add( helpAbout );

        return helpMenu;
    }
    */

    // Closes the client frame from a listener (e.g. File->Exit).
    class ExitClient implements ActionListener {
        public void actionPerformed( ActionEvent e ) {
            MenuBar.this.parentFrame.promptClose();
        }
    }

    // Opens 'Connect to New Project' frame.
    class ConnectToNewProject implements ActionListener {
        public void actionPerformed( ActionEvent e ) {
            LoginFrame loginFrame = new LoginFrame( MenuBar.this.parentFrame,
                    true );
            loginFrame.setLocationRelativeTo( MenuBar.this.parentFrame );
            loginFrame.setVisible( true );
        }
    }

    // Handles opening/loading of connection files.
    class OpenConnectionFile implements ActionListener {
        public void actionPerformed( ActionEvent e ) {
            JFileChooser fileChooser = new JFileChooser();
            FileNameExtensionFilter vwFilter = new FileNameExtensionFilter(
                    "Virtual World (*.vw)", "vw" );
            // FileNameExtensionFilter bevFilter = new FileNameExtensionFilter(
            // "Birds Eye View (*.bev)", "bev" );
            // fileChooser.setFileFilter( bevFilter );
            fileChooser.setFileFilter( vwFilter );

            if ( fileChooser.showOpenDialog( MenuBar.this.parentFrame ) == JFileChooser.APPROVE_OPTION ) {
                MenuBar.this.parentFrame.connectToDatabaseByFile( fileChooser
                        .getSelectedFile().getAbsolutePath() );
            }
        }
    }

    /* Not implemented
    // Opens the About frame.
    class AboutDialog implements ActionListener {
        public void actionPerformed( ActionEvent e ) {
            JDialog aboutFrame = new JDialog( MenuBar.this.parentFrame,
                    "About", true );
            aboutFrame.setSize( 400, 300 );
            aboutFrame.setResizable( false );
            aboutFrame.setLocationRelativeTo( MenuBar.this.parentFrame );
            aboutFrame.setVisible( true );

            JLabel testlabel = new JLabel();
            testlabel.setText( "Test Label!" );
        }
    }
    */
}
